﻿
using System;

using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace be.metanous.mvvm.converters
{
    public class BooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {

            var mode = parameter is BooleanConverterParameter
                           ? (BooleanConverterParameter) parameter
                           : BooleanConverterParameter.Inverse;
            switch (mode)
            {
                case BooleanConverterParameter.Inverse:
                    if (!(value is bool)) throw new NotImplementedException();
                    return !(bool)value;
                case BooleanConverterParameter.IsNull:
                    return (value == null) ? true : false;
                case BooleanConverterParameter.IsNotNull:
                    return (value != null) ? true : false;;
                case BooleanConverterParameter.IsUnset:
                    return (DependencyProperty.UnsetValue.Equals(value)) ? true : false;
                case BooleanConverterParameter.IsNotUnset:
                    return (!DependencyProperty.UnsetValue.Equals(value)) ? true : false;
                case BooleanConverterParameter.IsNullOrUnset:
                    return (value == null || DependencyProperty.UnsetValue.Equals(value)) ? true : false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var mode = parameter is BooleanConverterParameter
                          ? (BooleanConverterParameter)parameter
                          : BooleanConverterParameter.Inverse;
            switch (mode)
            {
                case BooleanConverterParameter.Inverse:
                    if (!(value is bool)) throw new NotImplementedException();
                    return !(bool)value;
                default:
                    throw new NotImplementedException();

            }
        }
    }
}
